package com.example.controller;

import com.example.service.SwaggerGenerationService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
public class SwaggerController {

    @Autowired
    private SwaggerGenerationService swaggerService;
    
    @Autowired
    private ObjectMapper objectMapper;

    // 暴露OpenAPI 3.0文档URL
    @GetMapping(value = "/v2/api-docs", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSwaggerJson() throws Exception {
        // 配置ObjectMapper以生成格式化的JSON输出
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        // 只排除值为null的字段，但保留指定的空数组/对象字段
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        
        OpenAPI openAPI = swaggerService.generateSwaggerDocument();
        return objectMapper.writeValueAsString(openAPI);
    }
}